#include "xingzuo.h"
#include "ui_xingzuo.h"
#include<QNetworkAccessManager>
#include<QNetworkReply>
#include<QNetworkRequest>
#include<QNetworkReply>
#include<QDebug>
#include<QJsonParseError>
#include<QJsonDocument>
#include<QJsonObject>
#include<QJsonValue>
#include<QStringList>

xingzuo::xingzuo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::xingzuo)
{
    ui->setupUi(this);
    setMouseTracking(true);

    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);

    manager=new QNetworkAccessManager(this);
    connect(manager,&QNetworkAccessManager::finished,this,&xingzuo::replydeal);

}

xingzuo::~xingzuo()
{
    delete ui;
}
void xingzuo::get(){
    req.setUrl(QUrl(path));
    manager->get(req);
}

void xingzuo::on_today_pressed()
{
    ui->stackedWidget->setCurrentIndex(0);
    string="today";
    str=ui->comboBox->currentText();
    path=QString("http[s]://route.showapi.com/872-1?showapi_appid=1000584&showapi_sign=2016c838afb54768bcbf4389c6624cab&star=baiyang");

    this->get();
}

void xingzuo::on_tomorrow_pressed()
{
    ui->stackedWidget->setCurrentIndex(1);
    string="tomorrow";
    str=ui->comboBox->currentText();
    path=QString("http[s]://route.showapi.com/872-1?showapi_appid=1000584&showapi_sign=2016c838afb54768bcbf4389c6624cab").arg(str);

    this->get();
}

void xingzuo::on_week_pressed()
{
    ui->stackedWidget->setCurrentIndex(2);
    string="week";
    str=ui->comboBox->currentText();
    path=QString("http://web.juhe.cn/constellation/getAll?consName=%1&type=week&key=d4a5e89d6ec2df07cfe39933be856927").arg(str);

    this->get();
}

void xingzuo::on_month_pressed()
{
    ui->stackedWidget->setCurrentIndex(3);
    string="month";
    str=ui->comboBox->currentText();
    path=QString("http://web.juhe.cn/constellation/getAll?consName=%1&type=month&key=d4a5e89d6ec2df07cfe39933be856927").arg(str);

    this->get();
}


void xingzuo::on_year_pressed()
{
    ui->stackedWidget->setCurrentIndex(4);
    string="year";
    str=ui->comboBox->currentText();
    path=QString("http://web.juhe.cn/constellation/getAll?consName=%1&type=year&key=d4a5e89d6ec2df07cfe39933be856927").arg(str);

    this->get();
}

void xingzuo::replydeal(QNetworkReply *reply){

    qDebug()<<string;
    QByteArray str;
    str=reply->readAll();
    if(string=="today"){
        this->jsondeal_to(str);
    }
    if(string=="tomorrow"){
        this->jsondeal_tom(str);
    }
    if(string=="week"){
        this->jsondeal_week(str);
    }
    if(string=="month"){
        this->jsondeal_mon(str);
    }
    if(string=="year"){
        this->jsondeal_year(str);
    }
}

void xingzuo::jsondeal_tom(QByteArray str){
    qDebug()<<2;
    QJsonParseError error;
    QJsonDocument doc=QJsonDocument::fromJson(str,&error);
    QJsonObject ob=doc.object();
    if(error.error==QJsonParseError::NoError){
        QJsonValue datetime=ob.value("datetime");
        datetimestr=datetime.toString();
        ui->tom_time->setText(datetimestr);
        QJsonValue all=ob.value("summary_star");
        allstr=all.toString();
        qDebug()<<allstr;
        QJsonValue color=ob.value("color");
        colorstr=color.toString();
        QJsonValue health=ob.value("health");
        healthstr=health.toString();
        QJsonValue love=ob.value("love");
        lovestr=love.toString();
        QJsonValue money=ob.value("money");
        moneystr=money.toString();
        QJsonValue number=ob.value("number");
        numberstr=number.toString();
        QJsonValue qfriend=ob.value("QFriend");
        qfriendstr=qfriend.toString();
        QJsonValue summary=ob.value("summary");
        summarystr=summary.toString();
        QJsonValue work=ob.value("work");
        workstr=work.toString();
        ui->tom_all->setText(QString("综合运势:"+allstr));
        ui->tom_love->setText(QString("爱情运势:"+lovestr));
        ui->tom_color->setText(QString("幸运颜色:"+colorstr));
        ui->tom_money->setText(QString("财富运势:"+moneystr));
        ui->tom_qfriend->setText(QString("速配星座:"+qfriendstr));
        ui->tom_work->setText(QString("工作运势:"+workstr));
        ui->tom_health->setText(QString("健康运势:"+healthstr));
        ui->tom_num->setText(QString("幸运数字:"+numberstr));
        ui->tom_text->setText(summarystr);


    }
}
void xingzuo::jsondeal_to(QByteArray str){
    qDebug()<<1;
    QJsonParseError error;
    QJsonDocument doc=QJsonDocument::fromJson(str,&error);
    QJsonObject ob=doc.object();
    if(error.error==QJsonParseError::NoError){
        QJsonValue datetime=ob.value("datetime");
        datetimestr=datetime.toString();
        ui->to_time->setText(datetimestr);
        QJsonValue all=ob.value("summary_star");
        allstr=all.toString();
        QJsonValue color=ob.value("color");
        colorstr=color.toString();
        QJsonValue health=ob.value("health");
        healthstr=health.toString();
        QJsonValue love=ob.value("love");
        lovestr=love.toString();
        QJsonValue money=ob.value("money");
        moneystr=money.toString();
        QJsonValue number=ob.value("number");
        numberstr=number.toString();
        QJsonValue qfriend=ob.value("QFriend");
        qfriendstr=qfriend.toString();
        QJsonValue summary=ob.value("summary");
        summarystr=summary.toString();
        QJsonValue work=ob.value("work");
        workstr=work.toString();
        qDebug()<<numberstr;
        ui->to_all->setText(QString("综合运势:"+allstr));
        ui->to_love->setText(QString("爱情运势:"+lovestr));
        ui->to_color->setText(QString("幸运颜色:"+colorstr));
        ui->to_money->setText(QString("财富运势:"+moneystr));
        ui->to_qfriend->setText(QString("速配星座:"+qfriendstr));
        ui->to_work->setText(QString("工作运势:"+workstr));
        ui->to_health->setText(QString("健康运势:"+healthstr));
        ui->to_number->setText(QString("幸运数字:"+numberstr));
        ui->to_text->setText(summarystr);


    }
}
void xingzuo::jsondeal_week(QByteArray str){
    qDebug()<<3;
    QJsonParseError error;
    QJsonDocument doc=QJsonDocument::fromJson(str,&error);
    QJsonObject ob=doc.object();
    if(error.error==QJsonParseError::NoError){
        QJsonValue datetime=ob.value("date");
        datetimestr=datetime.toString();
        ui->week_time->setText(datetimestr);
        QJsonValue health=ob.value("health");
        healthstr=health.toString();
        QJsonValue love=ob.value("love");
        lovestr=love.toString();
        QJsonValue money=ob.value("money");
        moneystr=money.toString();
        QJsonValue work=ob.value("work");
        workstr=work.toString();
        QJsonValue job=ob.value("job");
        jobstr=work.toString();
        QJsonValue week=ob.value("weekth");
        weekstr=work.toString();

        ui->week_love->setText(lovestr);

        ui->week_money->setText(moneystr);

        ui->week_work->setText(workstr);
        ui->week_health->setText(healthstr);
        ui->week_job->setText(jobstr);
        ui->weekth->setText(weekstr);





    }
}
void xingzuo::jsondeal_mon(QByteArray str){
    QJsonParseError error;
    QJsonDocument doc=QJsonDocument::fromJson(str,&error);
    QJsonObject ob=doc.object();
    if(error.error==QJsonParseError::NoError){
        QJsonValue datetime=ob.value("date");
        datetimestr=datetime.toString();
        ui->mon_time->setText(datetimestr);
        QJsonValue health=ob.value("health");
        healthstr=health.toString();
        QJsonValue love=ob.value("love");
        lovestr=love.toString();
        QJsonValue money=ob.value("money");
        moneystr=money.toString();
        QJsonValue work=ob.value("work");
        workstr=work.toString();
        QJsonValue all=ob.value("all");
        allstr=all.toString();

        ui->mon_love->setText(lovestr);

        ui->mon_money->setText(moneystr);

        ui->mon_work->setText(workstr);
        ui->mon_health->setText(healthstr);
        ui->mon_all->setText(allstr);



    }
}
void xingzuo::jsondeal_year(QByteArray str){
    QJsonParseError error;
    QJsonDocument doc=QJsonDocument::fromJson(str,&error);
    QJsonObject ob=doc.object();
    if(error.error==QJsonParseError::NoError){
        QJsonValue datetime=ob.value("date");
        datetimestr=datetime.toString();
        ui->year_time->setText(datetimestr);
        QJsonValue love=ob.value("love");
        lovestr=love.toString();
        QJsonValue money=ob.value("finance");
        moneystr=money.toString();
        QJsonValue work=ob.value("career");
        workstr=work.toString();

        ui->mon_love->setText(lovestr);

        ui->mon_money->setText(moneystr);

        ui->mon_work->setText(workstr);



    }
}



void xingzuo::on_close_clicked()
{
    this->close();
}

void xingzuo::on_min_clicked()
{
    this->showMinimized();
}
void xingzuo::mousePressEvent(QMouseEvent *e){
    if(e->button()==Qt::LeftButton){
        p=e->globalPos()-this->frameGeometry().topLeft();
    }
    else if(e->button()==Qt::RightButton){
        return;
    }
}
void xingzuo::mouseMoveEvent(QMouseEvent *e){
    if(e->buttons()==Qt::LeftButton){//获取的长时间的按键是左键
        this->move(e->globalPos()-p);
    }
}
